<%
if (session.getAttribute("username") == null) {
	%><jsp:forward page="login.jsp" /><%
}
%>
<%
String messsql = "Update Message SET Unread=0 WHERE Message.MessageID IN (SELECT Message.MessageID FROM Message, User_Msg WHERE User_Msg.MsgID = Message.MessageID AND User_Msg.Username = '"+(String)session.getAttribute("username")+"' )";
try{
	DbConnection messdb = new DbConnection();
	System.out.println(messsql);
	messdb.getConn().prepareStatement(messsql).executeQuery();
	messdb.close();
}catch(Exception e){
	//eh
}
%>
<jsp:useBean id="user" class="bean.PollUser" scope="request" />
<jsp:setProperty name="user" property="*"/>
<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="GBK" import="bean.PollUser, model.PollUserImpl, connection.DbConnection, java.sql.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"> 
<link rel="stylesheet" href="style/stylesheet.css" type="text/css" >
<title>dbPoll/Messages</title>
<script type="text/javascript">

    function check(form){   
		// Clear success msg
		document.getElementById("success_msg").innerHTML = "";
        
        if(document.forms.form1.define_poll.value==""){
            document.getElementById("define_poll_msg").innerHTML = "<font color=red>* Please add poll definition.</font>";
            document.forms.form1.define_poll.focus();   
            return false;   
        } else {
        	document.getElementById("define_poll_msg").innerHTML = "";
        }
        return true;
    }

    function selected_pri_pub(arg) {
		// Clear success msg
		document.getElementById("success_msg").innerHTML = "";
        
		// Split into public private
		if (arg == 1) {
			document.getElementById("public_private_row").innerHTML = "Please Select User";
			document.getElementById("public_private_poll").style.visibility = "hidden";
			document.getElementById("public_private_user").style.visibility = "visible";
			
		}
		if (arg == 2) {
			document.getElementById("public_private_row").innerHTML = "Please Select Poll";
			document.getElementById("public_private_user").style.visibility = "hidden";
			document.getElementById("public_private_poll").style.visibility = "visible";
		}
    }

    function validate_message_form(form) {
		// Clear success msg
		document.getElementById("success_msg").innerHTML = "";
        
		// Public_private not selected
    	if (!document.forms.message_form.public_private[0].checked && !document.forms.message_form.public_private[1].checked) {
			document.getElementById("public_private_message").innerHTML = "<div class=\"error\" style=\"display:block\"> * Please select private or public message. </div>";
			return false;
    	} else {
    		document.getElementById("public_private_message").innerHTML = "";
		}
		if (document.getElementById("messageTextInput").value == "") {
			document.getElementById("textAreaMessage").innerHTML = "<div class=\"error\" style=\"display:block\"> * Please input message text. </div>";
			return false;
		} else {
			document.getElementById("textAreaMessage").innerHTML = "";
		}
		return true;
    }

</script>
</head>

<body onload="user_type();">
<div class="bg">
<div class='content' >
<jsp:include page="nav.jsp" />
	<div class= "pagetitle">Messages</div>
	
	<div class="entrytitle">
		<div class="entry">
		<div class="tableborder"><table class="blockTable">
		<thead><tr><td>Received Time</td><td>Sender</td><td>Message Content</td></tr></thead>
		<tbody>
		<%	
			DbConnection db = new DbConnection();
			Connection conn = db.getConn();
			PreparedStatement ps;
			String sql = "Select * from Message,User_Msg where MessageID=MsgID AND User_Msg.Username = '"+session.getAttribute("username")+"' order by CreateTime DESC";
			
			ps = conn.prepareStatement(sql);
			ResultSet res = ps.executeQuery();
			//ResultSet rs = db.doSelect(sql);
			boolean hasMessages = false;
			while (res.next()) {	
				hasMessages = true;
				out.println("<tr><td style=\"width:10%\" id="+res.getString("MessageId")+">"+res.getString("CreateTime")+"</td><td style=\"width:5%\">"+res.getString("Sender")+"<td style=\"margin-left: 10px;\" colspan=\"2\">"+res.getString("text")+"</td></tr>");
			}
			if (!hasMessages) {
				out.println("<tr><td colspan = \"2\">You have no messages.</td></tr>");
			}
		%>
		</tbody>			
		</table>
		</div>
		</div>
	</div>
	
	<div class="entrytitle">
	Compose Message
		<div class="entry">
		<div class="tableborder"><table class="blockTable">
		<form action="sendMessageServlet" method="post" name="message_form" >
		<tr>
			<td style="width:5%"><input type="radio" name="public_private" id="public_private" value="private" checked onclick="selected_pri_pub(1);" />Private</td>
			<td style="width:5%"><div id="public_hide" <% if (session.getAttribute("accessibility") != null && Integer.parseInt(session.getAttribute("accessibility").toString()) == 1) { 	%>style="visibility:hidden;" <% } %> ><input type="radio" name="public_private" id="public_private" value="public" onclick="selected_pri_pub(2);" />Public</div></td>
			<td id="public_private_message" ></td>
		</tr>
		<tr><td><div id="public_private_row" >Please Select User</div></td><td>
			<div id="public_private_poll" style="visibility:hidden;" >
			<% 	
				// All roles > PM can send public to any poll
				String sql2 = "";
				if (session.getAttribute("accessibility") != null && Integer.parseInt(session.getAttribute("accessibility").toString()) > 2) {
					sql2 = "Select name from Poll";
				} else {
					sql2 = "Select name from Poll,User_Poll where username='"+session.getAttribute("username")+"' AND Poll.ID=User_Poll.PollID";
				}
				//System.out.println(sql2);
				ResultSet res2 = conn.prepareStatement(sql2).executeQuery();
				out.println("<select name=\"poll_list\" >");
				//ResultSet rs2 = db2.doSelect(sql2); 
				while (res2.next()) { 
					out.println("<option value=\""+res2.getString(1)+"\">"+res2.getString(1)+"</option>"); 
				}
				out.println("</select>");
				
			%>
			</div>
			<div id="public_private_user" style="visibility:visible;">
			<% 
			
				//DbConnection db4 = new DbConnection();
			
				String sql4 = "Select username from PollUser";
				ResultSet res3 = conn.prepareStatement(sql4).executeQuery();
				out.println("<select name=\"user_list\" >");
				//ResultSet rs4 = db4.doSelect(sql4);
				while (res3.next()) {
					out.println("<option value=\""+res3.getString(1)+"\">"+res3.getString(1)+"</option>");
				}
				out.println("</select>");
				db.close();
			%>
			</div>
		</td><td></td></tr>
		<tr><td>Message...</td><td><textarea id="messageTextInput" name="messageTextInput" rows="5" cols="50"></textarea></td><td id="textAreaMessage" ></td></tr>
		<tr><td></td><td><input type="submit" class="button" value="Send Message" onclick="return validate_message_form(this);" /></td><td></td></tr>
		</form>
		</table></div>
		<div id="success_msg">
		<%
			if (session.getAttribute("Message_send_success") != null && session.getAttribute("Message_send_success").equals("t")) {
				out.println("<div class=\"note\" style=\"display:block\" > Message successfully sent. </div>");
			}
		%>
		</div>
		</div>
	</div>

</div>



</div>
</body>
</html>